/**
 * <p>Title: CardServiceImpl.java</p>
 * <p>Description: </p> 
 * @author xuchang 
 * @date 2018年8月7日
 */
package com.bclz.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bclz.bo.PostBo;
import com.bclz.common.base.AbstractBaseService;
import com.bclz.entity.Post;
import com.bclz.entity.Tag;
import com.bclz.mapper.PostMapper;
import com.bclz.mapper.TagMapper;
import com.bclz.service.PostService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**  
* Title: PostServiceImpl  
* Description:   
* @author xuchang  
* @date 2018年8月7日  
*/

@Service
public class PostServiceImpl extends AbstractBaseService<Post, PostMapper> implements PostService {

	@Autowired
	private TagMapper tagMapper;
	
	@Transactional
	public void save(PostBo post) throws Exception {
		// TODO Auto-generated method stub
		List<String> tagIds=new ArrayList<>();
		String[] tags=post.getTags().split(",");
		for (String name : tags) {
			Tag tag=new Tag();
			tag.setName(name);
			tagMapper.save(tag);
			tagIds.add(tag.getId());
			tag=null;
		}
		post.setTagIds(tagIds);
		mapper.save(post);
	}

	@Override
	public PageInfo<PostBo> list(PostBo post) {
		// TODO Auto-generated method stub
		Page<PostBo> pageres=PageHelper.startPage(post.getPageNum(),post.getPageSize() , true);
		if(StringUtils.isNoneBlank(post.getTags())) {
			post.setTags("%"+post.getTags()+"%");
		}
		mapper.list(post);
		PageInfo<PostBo> info=new PageInfo<PostBo>(pageres);
		return info;
	}

	@Override
	public PostBo get(String id) {
		// TODO Auto-generated method stub
		return mapper.get(id);
	}

		

}
